package cn.anquing.util;

import java.math.BigDecimal;
import java.text.ParseException;


public class Util {

	/**
	 * double 相加，这样不会损失精度,只保留两位小数
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static double add(double d1, double d2) {
		BigDecimal bd1 = new BigDecimal(Double.toString(d1));
		BigDecimal bd2 = new BigDecimal(Double.toString(d2));
		return Double.valueOf(bd1.add(bd2).doubleValue());
	}

	/**
	 * double 相减，这样不会损失精度,只保留两位小数
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static double sub(double d1, double d2) {
		BigDecimal bd1 = new BigDecimal(Double.toString(d1));
		BigDecimal bd2 = new BigDecimal(Double.toString(d2));
		return Double.valueOf(bd1.subtract(bd2).doubleValue());
	}

	/**
	 * double 相乘，这样不会损失精度,只保留两位小数
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */

	public static double mul(double d1, double d2) {
		BigDecimal bd1 = new BigDecimal(Double.toString(d1));
		BigDecimal bd2 = new BigDecimal(Double.toString(d2));
		// return Double.valueOf(getDouble(bd1.multiply(bd2).doubleValue()));
		return Double.valueOf(bd1.multiply(bd2).doubleValue());
	}

	/**
	 * 相除,这样不会损失精度,只保留两位小数
	 * 
	 * @param d1
	 * @param d2
	 * @param len
	 * @return
	 */
	public static double div(double d1, double d2) {
		BigDecimal b1 = new BigDecimal(Double.toString(d1));
		BigDecimal b2 = new BigDecimal(Double.toString(d2));
		return Double.valueOf(b1.divide(b2, 7, BigDecimal.ROUND_HALF_UP).doubleValue());
	}

	/**
	 * 把double转为只剩两位小数点的String,用来显示。
	 * 
	 * @param a
	 * @return
	 */
	public static String double2String(double d) {
		String temp = d + "";
		String[] t = temp.split("\\.");
		String dian = t[1];

		if (dian.length() == 1) {
			temp = temp + "0";
		} else if (dian.length() >= 2) {
			temp = t[0] + "." + dian.substring(0, 2);

		}
		return temp;
	}

	/**
	 * 把double转为只剩两位小数点的double,用来计算。
	 * 
	 * @param d
	 * @return
	 */
	public static Double double2Double(double d) {

		return Double.valueOf(getDouble(d));

	}

	/**
	 * 把double转为只剩两位小数点的double,用来显示。
	 * 
	 * @param a
	 * @return
	 */
	public static String getDouble(double d) {
		String temp = d + "";
		String[] t = temp.split("\\.");
		String dian = t[1];

		if (dian.length() == 1) {
			temp = temp + "0";
		} else if (dian.length() >= 2) {
			temp = t[0] + "." + dian.substring(0, 2);
		}
		return temp;
	}

	/**
	 * 手机号码格式化182****5716
	 * 
	 * @param mobile
	 * @return
	 */
	public static String mobileFormat(String mobile) {
		String reStr = mobile.substring(7, 11);
		String preStr = mobile.substring(0, 3);
		StringBuilder sb = new StringBuilder();
		sb.append(preStr).append("****").append(reStr);
		return sb.toString();
	}

	/**
	 * 手机号码格式化182*****716
	 * 
	 * @param mobile
	 * @return
	 */
	public static String mobileFormatBy5Star(String mobile) {
		String reStr = mobile.substring(8, 11);
		String preStr = mobile.substring(0, 3);
		StringBuilder sb = new StringBuilder();
		sb.append(preStr).append("*****").append(reStr);
		return sb.toString();
	}

	/**
	 * 银行账号格式化6214********3138
	 * 
	 * @param str
	 * @return
	 */
	public static String bankNumberFormat(String str) {
		String reStr = str.substring(str.length() - 4, str.length());
		String preStr = str.substring(0, 4);
		StringBuilder sb = new StringBuilder();
		sb.append(preStr).append("********").append(reStr);
		return sb.toString();
	}

	public static Double getPenalty(Double money, Double interestRate, int day) {

		Double temp = Util.mul(Util.mul(money, 0.0005), day);
		Double a = Util.mul(1000d, interestRate);
		Double b = Util.mul(a, 1.5d);
		Double c = Util.mul(b, day);
		Double d = Util.div(c, 30d);
		return Util.add(temp, d);

		/*
		 * Double temp = money;; for(int i=0;i<day;i++){ Double inte =
		 * mul(money,0.0005); money = add(money,inte); } return sub(money,temp);
		 */

	}

	public static Double getLastLittleMoney(Double money, int periods) {

		Double a = Util.double2Double(Util.div(money, periods));
		Double b = Util.double2Double(Util.mul(a, periods));
		Double c = Util.double2Double(Util.sub(money, b));

		return c;
	}

	public static double getMoneyByScore(int score) {
		// int score = 699;
		double money = 0d;

		if (score > 990)
			money = 30000d;

		else if (score > 960)
			money = 29000d;

		else if (score > 940)
			money = 28000d;

		else if (score > 920)
			money = 27000d;

		else if (score >= 900)
			money = 26000d;

		else if (score > 840)
			money = 25000d;

		else if (score >= 800)
			money = 24000d;

		else if (score > 780)
			money = 23000d;

		else if (score > 770)
			money = 22000d;

		else if (score > 760)
			money = 21000d;

		else if (score > 750)
			money = 20000d;

		else if (score > 730)
			money = 19000d;

		else if (score >= 700)
			money = 18000d;

		else if (score > 660)
			money = 17000d;

		else if (score > 630)
			money = 16000d;

		else if (score > 599)
			money = 15000d;

		else if (score > 590)
			money = 14000d;

		else if (score > 580)
			money = 13000d;

		else if (score > 570)
			money = 12000d;

		else if (score > 560)
			money = 11000d;

		else if (score > 550)
			money = 10000d;

		else if (score > 540)
			money = 9000d;

		else if (score > 530)
			money = 8000d;

		else if (score > 520)
			money = 7000d;

		else if (score > 510)
			money = 6000d;

		else if (score >= 500)
			money = 5000d;
		else
			money = 0d;

		// System.out.print(money);
		return money;
	}

	public static void main(String[] args) throws ParseException {/*
																 * 
																 * List<String >
																 * a = new
																 * ArrayList <
																 * String>() ;
																 * a.add("go" );
																 * List<String >
																 * b = new
																 * ArrayList <
																 * String>() ;
																 * a.add("run"
																 * ); a.addAll
																 * (b);
																 * 
																 * for(String
																 * str:a){
																 * System .out
																 * .println
																 * (str); }
																 */

		//
		// JSONArray jb = JSONArray.parseArray("[{\"a\":\"b\"},{\"c\":\"d\"}]");
		//
		// Integer a = Integer.valueOf("1");

		/*
		 * boolean a = false; boolean b = false; boolean c = false; boolean d =
		 * false;
		 * 
		 * if(a!=a)
		 */

		// System.out.print("【嘀嗒分期】注册验证码：******，15分钟内有效，请尽快提交验证。如有疑问请联系客服0757-63218388。".length());
		// System.out.print("【嘀嗒分期】注册验证码：******，15分钟内有效，请尽快提交验证。如有疑问请联系客服0757-63218388。".length());
		// System.out.print("【嘀嗒分期】注册验证码：******，15分钟内有效，请尽快提交验证。如有疑问请联系客服0757-63218388。".length());
		// System.out.print("【嘀嗒分期】注册验证码：******，15分钟内有效，请尽快提交验证。如有疑问请联系客服0757-63218388。".length());

		// "(^\\d{18}$)|(^\\d{15}$)";

	}

}
